let wxCharts = require('../../utils/wxcharts.js');
let lineChart = null;
Page({
  data: {

  },
  onUnload: function () {
    let back = this.data.back;
    if (back) {
      wx.navigateBack({
        delta: back
      })
    }
    
  },

  onLoad: function (options) {
    
    let windowWidth = 320;
    try {
      let res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
      this.setData({ windowWidth })
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    let username = wx.getStorageSync('username');
    if (options.test) {
      let test = JSON.parse(options.test);
      this.codeData(test);

    }else {
      let quesName = options.quesName;
      let ans = JSON.parse(options.ans);
      this.setData({
        quesName,
        ans,
        username,
        back: 3
      })

      this.quesAns(options)
        .then(this.codeData.bind(this));
    }
  },

  // 发送请求
  quesAns(o) {
    return new Promise(resolve => {
      wx.request({
        url: 'http://47.107.121.61/user/quesAns',
        data: {
          quesName: this.data.quesName,
          ans: this.data.ans,
          username: this.data.username
        },
        method: "POST",
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          const status = res.data.status;
          resolve(res);
        }
      })
    })
  },

  // history(o) {
  //   return new Promise(resolve => {
  //     wx.request({
  //       url: 'http://localhost/user/history',
  //       data: {
  //         username: this.data.username,
  //       },
  //       method: "GET",
  //       header: {
  //         'content-type': 'application/json'
  //       },
  //       success: res => {
  //         const status = res.data.status;
  //         resolve(res);
  //       }
  //     })
  //   })
  // },

  // 处理分析数据
  codeData(res) {
    let X = [];
    let Tn = [];
    if (res.data) {
      X = res.data.X;
      Tn = res.data.Tn;
    }else {
      X = res.X;
      Tn = res.Tn;
    }

    let x = [];
    let tn = [];
    let sd_upper = [];
    let sd_lower = [];
    for (let i = 0; i < Tn.length; i++) {
      let T = "T" + (i + 1);
      tn.push(Tn[i][T])
      x.push(X[i].x);
      sd_upper.push(X[i].x + X[i].sd);
      sd_lower.push(X[i].x - X[i].sd)
    }
    this.setData({
      x,
      tn,
      Tn,
      sd_upper,
      sd_lower
    })

    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: ['躯体化', '强迫', '人际关系', '抑郁', '焦虑', '敌对性', '恐惧', '偏执', '精神病性', '睡眠、饮食及其他'],
      animation: true,
      series: [{
        name: '测试者T分',
        data: tn,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }, {
        name: '常模T分平均数',
        data: x,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }, {
        name: '常模标准差上限',
        data: sd_upper,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }, {
        name: '常模标准差下限',
        data: sd_lower,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '因子均分',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0,
        max: 4
      },
      width: this.data.windowWidth,
      height: 400,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },

  touchHandler: function (e) {
    lineChart.scrollStart(e);
  },

  touchEndHandler: function (e) {
    lineChart.scrollEnd(e);
    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
})